﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChangeHope.Common;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {
        public string ScanInfo()
        {
            string mystyle = this.Label_Inserted;//标签显示内容
            string str_ShowType = this.GetParamValue("YX:ShowNumber");//显示数量
            string TempletLayout = this.GetParamValue("YX:TempletLayout");//布局方式
            string ShowRowsC = this.GetParamValue("YX:ShowRows");//行
            string ScanType = this.GetParamValue("YX:ScanType");//类别
            string WhetherOmit = this.GetParamValue("YX:WhetherOmit");
            int ShowRows = 1;//显示行
            if (!string.IsNullOrEmpty(ShowRowsC))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(ShowRowsC))
                {
                    ShowRows = Convert.ToInt32(ShowRowsC);
                }
            }
            string ShowColumnsC = this.GetParamValue("YX:ShowColumns");//列
            int ShoeColumns = 1;//显示列
            if (!string.IsNullOrEmpty(ShowColumnsC))
            {
                if (ChangeHope.Common.ValidateHelper.IsNumber(ShowColumnsC))
                {
                    ShoeColumns = Convert.ToInt32(ShowColumnsC);
                }
            }
            string productId = ChangeHope.WebPage.PageRequest.GetQueryString("q_productid");
            StringBuilder shtml = new StringBuilder();
            int count = 0;
            string strcontent = string.Empty;
            if(productId!="")
            {   

                List<YXShop.Model.Product.ProductInfo> list = null;
                if (ScanType == "0")//浏览该商品还浏览记录
                {
                    list = GetListByScanProductId(Convert.ToInt32(productId));
                }
                else
                {
                    list = GetScanBuyListByProductId(Convert.ToInt32(productId));
                }
                int r = 0;
                if(list!=null&&list.Count>0)
                {

                    if (str_ShowType=="all")
                    {
                        count = list.Count;
                    }
                    else if (Convert.ToInt32(str_ShowType) > list.Count)
                    {
                        count = list.Count;
                    }
                    else
                    {
                        count = Convert.ToInt32(str_ShowType);
                    }
                    YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                  
                    for (int i = 0; i < count; i++)
                    {
                            strcontent = mystyle;
                            if (strcontent.IndexOf("[#Product_No]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Product_No]", list[i].pro_NO.ToString());
                            }
                            if (strcontent.IndexOf("[#Product_Unit]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Product_Unit]", list[i].pro_Unit.ToString());
                            }
                            int NameCharacterNu = 8;
                            if (!string.IsNullOrEmpty(str_ShowType) && ValidateHelper.IsNumber(str_ShowType))
                            {
                                NameCharacterNu = Convert.ToInt32(str_ShowType);
                            }
                            string Ellipsis = "...";
                            if (WhetherOmit == "False")
                            {
                                Ellipsis = "";
                            }
                            if (strcontent.Contains("[#Product_Name]"))
                            {
                                strcontent = strcontent.Replace("[#Product_Name]", "" + ChangeHope.Common.StringHelper.SubStringAndAppend(list[i].pro_Name.ToString(), NameCharacterNu, Ellipsis) + "");//商品名称

                            }
                            if (strcontent.IndexOf("[#Product_ShopPrice]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Product_ShopPrice]", "￥" +Convert.ToDouble(list[i].pro_ShopPrice).ToString("f2"));
                            }
                            if (strcontent.IndexOf("[#Product_MarketPrice]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Product_MarketPrice]","￥"+Convert.ToDouble(list[i].pro_MarketPrice).ToString("f2"));
                            }
                            if (strcontent.IndexOf("[#Product_Thumbnails]") > -1)
                            {
                                strcontent = strcontent.Replace("[#Product_Thumbnails]", sp.DummyPaht+ list[i].pro_Thumbnail.ToString());
                            }
                            if (strcontent.Contains("[#DummyPahtAndTemplatePaht]"))
                            {
                                strcontent = strcontent.Replace("[#DummyPahtAndTemplatePaht]", sp.DummyPaht+sp.WebSiteTemplatePath);
                            }
                            if (strcontent.IndexOf("[#Product_Url]") > -1)
                            {
                                string url = YXShop.TemplateAction.Common.UrlStr("productcontent", "q_productid=" + list[i].pro_ID, "product/");
                                strcontent = strcontent.Replace("[#Product_Url]", url);
                            }

                            if (TempletLayout == "0")
                            {
                                shtml.Append(strcontent);
                            }
                            else
                            {
                                r++;
                                shtml.Append(strcontent);
                                if (ShoeColumns % r == 0)
                                {
                                    shtml.Append("</tr><tr>");
                                }
                            }   
                     }
             
                }
            }
            return shtml.ToString();
        }

        /// <summary>
        /// 根据商品ID查看浏览过该商品的用户还浏览过的商品集合
        /// </summary> 
        /// <param name="productId"></param>
        /// <returns></returns>
        private List<YXShop.Model.Product.ProductInfo> GetListByScanProductId(int productId)
        {
            YXShop.BLL.Product.ScanInfo scanBll = new YXShop.BLL.Product.ScanInfo();
            List<YXShop.Model.Product.ScanInfo> scanList = scanBll.GetListByWhere(" productId="+productId);//根据商品ID查中间表
            YXShop.BLL.Product.ProductInfo infoBll = new YXShop.BLL.Product.ProductInfo();
            List<YXShop.Model.Product.ProductInfo> productList = new List<YXShop.Model.Product.ProductInfo>();
            List<int> userIds = new List<int>();
            if(scanList != null && scanList.Count > 0)
            {
                foreach (YXShop.Model.Product.ScanInfo item in scanList)
                {
                    userIds.Add(item.Uid);
                }
            }
            if(userIds != null && userIds.Count > 0)
            {
                foreach (int item in userIds)
	            {
                    List<YXShop.Model.Product.ScanInfo> list = scanBll.GetListByWhere(" uid="+item);
                    if(list!=null&&list.Count>0)
                    {
                        foreach (YXShop.Model.Product.ScanInfo info in list)
                        {
                            if(info.ProductId==productId)
                            {
                                continue;//不记录该商品
                            }
                            YXShop.Model.Product.ProductInfo model = infoBll.GetModel(info.ProductId);
                            if(model!=null)
                            {   
                                if(productList.Contains(model))
                                {
                                    continue;//不记录重复商品
                                }
                                productList.Add(model);
                            }
                        }
                    }

	            }       
            }
            return productList; 
        }
        /// <summary>
        /// 根据商品ID查购买过此商品的用户还浏览过的商品集合
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        private List<YXShop.Model.Product.ProductInfo> GetScanBuyListByProductId(int productId)
        {   
            YXShop.BLL.Product.ScanInfo scanBll = new YXShop.BLL.Product.ScanInfo();
            YXShop.BLL.Order.Orders orderBll = new YXShop.BLL.Order.Orders();
            YXShop.BLL.Order.OrderProduct opBll = new YXShop.BLL.Order.OrderProduct();
            ChangeHope.DataBase.DataByPage orderDataPage = opBll.GetListByPage(" and ProId="+productId);//根据商品ID查订单
            List<int> userIds = new List<int>();
            YXShop.BLL.Product.ProductInfo infoBll=new YXShop.BLL.Product.ProductInfo();
            List<YXShop.Model.Product.ProductInfo> infolist=new List<YXShop.Model.Product.ProductInfo>();
            if(orderDataPage.DataReader!=null)
            {
                while(orderDataPage.DataReader.Read())
                {
                    int orderId = Convert.ToInt32(orderDataPage.DataReader["OrderId"]);
                    YXShop.Model.Order.Orders order = orderBll.GetModel(orderId);
                    if(order!=null)
                    {
                        if(Convert.ToInt32(order.OrderStatus)==5)//交易完毕的订单
                        {
                            YXShop.BLL.Member.MemberAccount memberBll = new YXShop.BLL.Member.MemberAccount();
                            YXShop.Model.Member.MemberAccount model = memberBll.GetModel(order.UserId);
                            if(model!=null)
                            {
                                userIds.Add(model.UID);
                            }
                            
                        }
                    }
                }
            }
            if(userIds!=null&&userIds.Count>0)
            {
                foreach (int item in userIds)
                {
                    List<YXShop.Model.Product.ScanInfo> list = scanBll.GetListByWhere(" uid="+item);
                    if(list!=null&&list.Count>0)
                    {
                        foreach (YXShop.Model.Product.ScanInfo info in list)
                        {
                            if(info.ProductId==productId)
                            {
                                continue;
                            }
                            YXShop.Model.Product.ProductInfo model = infoBll.GetModel(info.ProductId);
                            if(model!=null)
                            {
                                if(infolist.Contains(model))
                                {
                                    continue;
                                }
                                infolist.Add(model);
                            }

                        }
                    }
                }
            }
            return infolist;           
        }
    }
 
}
